package com.hrbu.mp.generator.config;

import com.hrbu.mp.generator.converter.StringToEnumConverter;
import com.hrbu.mp.generator.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.PathResource;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Description: SrpingMvcConfig类用于替换SpringMVC.xml配置文件
 * @Author: tanyajun
 * @CreateTime: 2021/3/19
 * @Blog: https://tanyajun.top
 */
@Configuration // 声明本类是配置类
@ComponentScan(value = "com.hrbu.mp.generator"
        ,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class})})  // 注解扫描
@EnableWebMvc // 开启注解式web配置---相当于----<mvc:annotation-driven />标签
@Import(SpringConfig.class)
public class SrpingMVCConfig implements WebMvcConfigurer {
    // 添加视图解析器
    /*@Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // 设置前缀
        viewResolver.setPrefix("/WEB-INF/views/");
        // 设置后缀
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    // 添加文件解析器
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        // 设置默认编码集
        resolver.setDefaultEncoding("UTF-8");
        // 设置最大读取量
        resolver.setMaxInMemorySize(1024);
        // 设置最大上传容量
        resolver.setMaxUploadSize(1024000);
        return resolver;
    }*/
    /*-------------------------------------------------------------------------------------*/
    /**
     * 配置视图解析器
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/views/", ".jsp");
    }
    /**
     * 文件上传
     * @param webRoot 项目根路径
     * @return
     * @throws IOException
     */
    @Bean("multipartResolver")
    public CommonsMultipartResolver multipartResolver(
            @Value("#{systemProperties['webapp.root']}") String webRoot) throws IOException {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(1024 * 1024 * 5); // 最大上传大小
        resolver.setMaxUploadSizePerFile(1024 * 1024); // 每个最大大小
        resolver.setMaxInMemorySize(1024 * 1024); // 内存中最大大小
        resolver.setUploadTempDir(new PathResource(webRoot + "upload")); // 设置上传路径
        resolver.setDefaultEncoding("utf-8"); // 设置编码格式
        return resolver;
    }
    /**
     *  配置拦截器-使用自定义的拦截器拦截、枚举类型转换器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器、拦截路径、不拦截请求静态资源
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/hello").excludePathPatterns("/resources/**");
    }
    /**
     * 配置日期格式
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd"));
        // 添加枚举转换器
        registry.addConverterFactory(new StringToEnumConverter());
    }
    /**
     * 配置静态文件加载规则
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 开发环境-不缓存
        // addResourceHandler 添加资源请求地址
        // addResourceLocations 添加静态资源路径
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(0);
        /*// 生产环境配置
        // 强制缓存，通过资源路径加入MD5哈希值进行资源强制更新
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/")
                .setCachePeriod(31536000)
                .resourceChain(true)
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));*/
    }

}
